package com.lambda;

import java.util.function.*;

/**
 * 在Java中,函数式接口是只包含一个抽象方法的接口,它们是支持Lambda表达式的基础,因为Lambda表达式需要一个目标类型,这个目标类型必须是一个函数式接口
 *      函数式接口的出入参定义
 *          有入参,有出参
 *          有入参,没出参
 *          无入参,无出参
 *
 *
 */
public class Lambda04 {

    public static void main(String[] args) {
        //有入参,无出参  有参数,无返回值
        BiConsumer<String,String> biConsumer = new BiConsumer() {
            @Override
            public void accept(Object o, Object o2) {
                System.out.println("哈哈" + o + "嘿嘿" + o2);
            }
        };
        biConsumer.accept("3","4");

        //Lambda写法
        BiConsumer<String,String> biConsumer1 = (str1,str2) -> {
            System.out.println("哈哈" + str1 + "嘿嘿" + str2);
        };

        biConsumer.accept("1","2");


        //有入参,有出参  有参数,有返回值
        Function<String,String> function = new Function<String, String>() {
            @Override
            public String apply(String s) {
                return "Hello" + s;
            }
        };
        String str = function.apply("世界");
        System.out.println(str);

        //Lambda表达式
        Function<String,String> function2 = (String str1) -> {
            return "你好" + str1;
        };

        String str2 = function2.apply("世界");
        System.out.println(str2);


        BiFunction<String,Integer,Long> biFunction = (String str3, Integer integer) ->{
            return 8L;
        };

        Long hello = biFunction.apply("你好", 5);
        System.out.println(hello);


        //无入参  无出参  无参数,无返回值
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("hehehe");
            }
        };
        runnable.run();

        //Lambda表达式的写法
        Runnable runnable2 = () -> {
            System.out.println("heiehei");
        };

        runnable2.run();


        //无入参 有出参  无参数,有返回值
        Supplier supplier = new Supplier() {
            @Override
            public Object get() {
                return "hahaha";
            }
        };
        Object o = supplier.get();
        System.out.println(o);

        //Lambda表达式
        Supplier supplier1 = () ->{
            return "nihao";
        };

        Object o1 = supplier1.get();
        System.out.println(o1);


        //断言   判断入参的值是否正确
        Predicate<Integer> predicate = new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) {
                return integer % 2 == 0;
            }
        };
        boolean res = predicate.test(15);
        System.out.println(res);

        //Lambda表达式
        Predicate<Integer> predicate1 = (Integer integer) ->{
          return integer % 2 == 0;
        };

        boolean res1 = predicate1.test(10);
        System.out.println(res1);
    }
}
